<?php
session_start();
ob_start();

include_once "constants.php";
include_once "boot.php";


function __autoload($class) {
	$filename = uncamelize($class);
	// controllers
	if(file_exists(DIR_CONTROLLERS . DIR_DELIMITER . $filename . ".controller.php")){
		require_once DIR_CONTROLLERS . DIR_DELIMITER . $filename . ".controller.php";
	// models
	}else if(file_exists(DIR_MODELS . DIR_DELIMITER . preg_replace('/_model$/', "", $filename) . ".model.php")){
		require_once DIR_MODELS . DIR_DELIMITER . preg_replace('/_model$/', "", $filename) . ".model.php";
	// library
	}elseif(file_exists(DIR_LIBRARY . DIR_DELIMITER . $filename . ".class.php")){
		require_once DIR_LIBRARY . DIR_DELIMITER . $filename . ".class.php";
	}else{
		header("HTTP/1.0 GET 404 Not found");
		echo "<h1>Requested Class &quot;$class&quot; could not be instantinated.</h1>";
	}
}

/**
 * Dumps variable into output
 * @param Mixed $variable
 */
function pr($var) {
	echo '<pre>';
	if(is_object($var) || is_array($var)) {
		print_r($var);
	}elseif(is_bool($var) || empty($var)){
		var_dump($var);
	}else{
		echo $var;
	}
	echo '</pre>';
}
/**
 * Convert file_name_case string to CamelCase string
 * @param String $input_string
 * @return String $ReturnString
 */
function camelize($string){
	$ret = strtoupper(substr($string, 0, 1));
	for($i=1; $i<strlen($string); $i++){
		$c = substr($string, $i, 1);
		if($c=="_") {
			$ret .= strtoupper(substr($string, ++$i, 1));
		}else{
			$ret .= strtolower($c);
		}
	}
	return $ret;
}
/**
 * Convert CamelCase string to file_name_case string
 * @param String $InputString
 * @return String $return_string
 */
function uncamelize($string){
	$ret = "";
	for($i=0; $i<strlen($string); $i++) {
		$c = substr($string, $i, 1);
		if($i>0 && $c == strtoupper($c)) {
			$c = "_{$c}";
		}
		$ret .= strtolower($c);
	}
	return $ret;
}
/**
 * Get plural of a singular
 * @param String $singular
 */
function pluralize($singular){
	return Inflector::pluralize($singular);
}

function singularize($plural){
	return Inflector::singularize($plural);
}

function redirect($url=null) {
	if(!$url) {
		$url = $_SERVER["HTTP_REFERER"] ? $_SERVER["HTTP_REFERER"] : "/";
	}
	ob_clean();
	header("Location: $url");
	die;
}
?>